<template>
	<view>
		<view class="wrap">
			<u-search placeholder="请输入关键字" v-model="query.keyword" @search="searchKey" @custom="searchKey"></u-search>

			<!-- 			<view class="but-add">
				<u-button type="primary" text="新增商品模板"></u-button>
			</view> -->
			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom"
				v-if="skuList.length !== 0">
				<view class="page-box">
					<view class="order" v-for="(item, index) in skuList" :key="item.id">
						<view class="item">

							<view class="content">
								<view class="title u-line-2">{{item.skuCode}}</view>
								<view class="type">名称: {{ item.itemName }}</view>
								<view class="type">尺码: {{ item.itemSize }}</view>
								<view class="type">品牌: {{ item.itemBrand }}</view>
								<view class="type">成本价: {{ item.itemPrice }}</view>
								<view class="type">渠道来源: {{ item.source }}</view>
								<view class="type">描述: {{ item.itemDesc }}</view>
							</view>
							<view class="right">
								<image :src="item.itemImg" mode="aspectFill"></image>
							</view>
						</view>
						<view class="bottom">
							<view class="evaluate btn" @click="showModal">新增</view>
							<view class="evaluate btn" type="primary reverse" @click="getImage(item.id)">上传主图
							</view>
							<view class="evaluate btn" @click="updateStock(item)">
								{{item.buttonName?item.buttonName:"库存更新"}}
							</view>
							<view class="evaluate btn" @click="delSku(item.id)">删除</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
				</view>
			</scroll-view>
			<scroll-view scroll-y style="height: 100%;width: 100%;" v-else>
				<view class="page-box">
					<view>
						<view class="centre">
							<!-- <image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image> -->
							<view class="explain">
								您还没有相关商品模板
								<view class="tips">请点击新增商品模板进行创建</view>
							</view>
							<view class="btn"><u-button type="primary" text="新增商品模板" @click="showModal"></u-button>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>

	<u-modal :show="show" title="新增商品模板" content="内容" @confirm="confirm" @cancel="show=false" :showCancelButton="true"
		ref="uModal" :asyncClose="false">
		<up-form ref="form1" :model="addSkuInfo" label-width="80">
			<up-form-item label="编码" prop="skuCode" required>
				<up-input v-model="addSkuInfo.skuCode" placeholder="请输入编码" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="名称" prop="itemName" required>
				<up-input v-model="addSkuInfo.itemName" placeholder="请输入名称" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="尺码" prop="itemSize" required>
				<up-input v-model="addSkuInfo.itemSize" placeholder="请输入尺码" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="品牌" prop="itemBrand" required>
				<up-input v-model="addSkuInfo.itemBrand" placeholder="请输入品牌" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="单价" prop="itemPrice" required>
				<up-input v-model="addSkuInfo.itemPrice" placeholder="请输入单价" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="渠道来源" prop="source">
				<up-input v-model="addSkuInfo.source" placeholder="请输入渠道来源" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
			<up-form-item label="描述" prop="itemDesc">
				<up-input v-model="addSkuInfo.itemDesc" placeholder="请输入描述" type="textarea" border :height="100"
					auto-height />
			</up-form-item>
		</up-form>
	</u-modal>

	<view>
		<u-modal :show="stockShow" title="库存更新(销售管理产生相应记录)" content="内容" @confirm="stockConfirm"
			@cancel="stockShow=false" :showCancelButton="true" ref="uModal1" :asyncClose="true">
			<up-form ref="addStockForm">
				<u-form-item label="库存:" prop="addStockNum.num" borderBottom=true>
					<up-number-box v-model="addStockNum.num" integer @change="changeNumBox" :min="1" :max="50">
					</up-number-box>
				</u-form-item>
				<u-form-item label="尺码:" prop="addStockNum.itemSize" borderBottom=true>
					<u--input v-model="addStockNum.itemSize" border="none"></u--input>
				</u-form-item>
				<u-form-item label="价格:" prop="addStockNum.itemPrice" borderBottom=true>
					<u--input v-model="addStockNum.itemPrice" border="none"></u--input>
				</u-form-item>
			</up-form>
		</u-modal>
	</view>
</template>



<script>
	import {
		skuList,
		updateSkuImg,
		uploadImg,
		addSku,
		delSku,
		addSkuStock
	} from "@/api/work/product/product.js";

	export default {
		data() {
			return {
				nextPageFlag: true,
				query: {
					pageNum: 1,
					pageSize: 10,
					keyword: ''
				},
				show: false,
				stockShow: false,
				addStockNum: {
					num: 1
				},
				skuList: [],
				addSkuInfo: {
					skuCode: '',
					itemName: '',
					itemSize: '',
					itemBrand: '',
					itemPrice: '',
					source: '',
					itemDesc: ''
				},
				skuName: '',
				totalCount: 0,
				current: 0,
				swiperCurrent: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
				rules: {
					itemPrice: {
						required: true,
						type: 'number',
						message: '请填写成本价',
						trigger: ['blur', 'change']
					},
					skuCode: {
						required: true,
						message: '请填写编码',
						trigger: ['blur', 'change']
					},
					itemName: {
						required: true,
						message: '请填写名称',
						trigger: ['blur', 'change']
					},
					itemSize: {
						required: true,
						message: '请填写尺码',
						trigger: ['blur', 'change']
					},
					itemBrand: {
						required: true,
						message: '请填写品牌',
						trigger: ['blur', 'change']
					},
				}
			};
		},
		onLoad() {
			this.getSkuList();
		},
		computed: {

		},
		// watch: {
		// 	show(newVal){
		// 		if(newVal){
		// 			this.$nextTick(()=>{
		// 				this.$refs.form1.setRules(this.rules);
		// 			});
		// 		}
		// 	}
		// },
		methods: {
			confirm() {
				this.$refs.form1.setRules(this.rules);
				this.$refs.form1.validate().then(res => {
					addSku(this.addSkuInfo).then(response => {
						if (response.code == 200) {
							uni.$u.toast('修改成功')
							this.show = false;
							this.clearSkuInfo();
							setTimeout(() => {
								this.searchKey()
							}, 500);
						}
					})
				}).catch(errors => {
					console.info(errors)
					uni.$u.toast('校验失败')
				})
			},
			stockConfirm() {
				addSkuStock(this.addStockNum).then(response => {
					if (response.code == 200) {
						uni.$u.toast('修改成功')
						this.stockShow = false;
						this.clearSkuInfo();
						setTimeout(() => {
							this.searchKey()
						}, 500);
					}
				})
				this.addStockNum.num = 1;
				this.addStockNum.id = null;
			},
			showModal(item) {
				this.show = true;
				this.skuName = item;
			},
			changeNumBox(e) {
				console.log(e)
			},
			clearSkuInfo() {
				this.addSkuInfo.skuCode = ''
				this.addSkuInfo.itemImg = ''
				this.addSkuInfo.itemName = ''
				this.addSkuInfo.itemSize = ''
				this.addSkuInfo.itemBrand = ''
				this.addSkuInfo.itemPrice = ''
				this.addSkuInfo.source = ''
				this.addSkuInfo.itemDesc = ''
			},
			updateStock(item) {
				this.stockShow = true;
				this.addStockNum.id = item.id;
				this.addStockNum.itemSize = item.itemSize;
				this.addStockNum.itemPrice = item.itemPrice;
			},
			delSku(id) {
				let data = {
					'id': id
				}
				delSku(data).then(response => {
					if (response.code == 200) {
						uni.$u.toast('删除成功')
						setTimeout(() => {
							this.searchKey()
						}, 300);
					}
				})
			},
			// 触底懒加载
			reachBottom() {
				console.info("触底----开始加在skuList")
				this.loadStatus.splice(this.current, 1, "loading")
				if (this.nextPageFlag) {
					setTimeout(() => {
						console.info("延迟1.1s")
						this.getSkuList(this.current);
					}, 1100);
				}

			},
			getImage: function(id) {
				let parent = this
				uni.chooseImage({
					success: function(res) {
						let data = {
							name: 'file',
							filePath: res.tempFilePaths[0]
						}
						uploadImg(data).then(response => {
							let dataImg = {
								'id': id,
								itemImg: response.data.url
							}
							updateSkuImg(dataImg).then(response => {
								if (response.code == 200) {
									uni.$u.toast('上传成功')
									parent.searchKey()
								}
							})
						})
					},
				})
			},
			searchKey() {
				this.query.pageNum = 1
				this.skuList = []
				this.getSkuList()
			},
			// 页面数据
			getSkuList() {
				this.nextPageFlag = false;
				const pageTotal = this.totalCount / this.query.pageSize
				console.info('pageTotal:' + pageTotal)
				console.info('pageNum:' + this.query.pageNum)
				if (true) {
					setTimeout(() => {
						skuList(this.query).then(response => {
							this.skuList.push(...response.rows)
							this.totalCount = response.total
							//如果 rows.length<pageSize,说明没有下一页了
							if (response.rows.length < this.query.pageSize) {
								this.nextPageFlag = false;
							} else {
								this.query.pageNum++
								this.nextPageFlag = true;
							}
						})
					}, 500)
					this.loadStatus.splice(this.current, 1, "loadmore")
				} else {
					this.loadStatus.splice(this.current, 1, "noMore")
				}
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.but-add {
		width: 20%;
		margin-right: 20rpx;
		margin-left: 20rpx;
	}

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #2979ff;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #2979ff;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			flex-direction: row;
			margin: 20rpx 0 0;

			.right {
				margin-top: 100rpx;
				margin-right: 80rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				flex: 1;

				/* 占据剩余空间 */
				.title {
					font-size: 38rpx;
					line-height: 50rpx;
					margin-bottom: 10rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 28rpx;
				}
			}
		}

		.number-box {
			button-size: "5";
			color: "#ffffff";
			bgColor: "#2979ff";
			iconStyle: "color: #fff";
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>